/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db.HivDbIntegration.hivData;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import units.Gene;
import units.GeneLibrary;
import units.HivInteraction;
import units.Interaction;
import utils.Handle;

/**
 *
 * @author jamie
 */
public class HivData {

    private List<Gene> hivGenes = new ArrayList<Gene>();
    private List<HivInteraction> hivInteractions = new ArrayList<HivInteraction>();
    private List<Interaction> interactions = new ArrayList<Interaction>();
    private List<Gene> genes = new ArrayList<Gene>();

    public void build(GeneLibrary library, int minPmIds, boolean regulationOnly) {
        Handle handle = Handle.getConnectedHandle("hiv_interactions");
        String sql;
        if(!regulationOnly){
            sql = "select distinct gId_1, interaction, gId_2, pubmedId from hiv_interactions where gId_1!='155807'";
        } else {
            sql = "select distinct gId_1, interaction, gId_2, pubmedId from hiv_interactions where gId_1!='155807' and (interaction='upregulates' || interaction='downregulates' || interaction='regulates')";
        }
        ResultSet rs = handle.runSQLquery(sql);
        try {
            while (rs.next()) {
                int noOfPmIds = rs.getString("pubmedId").split(",").length;
//                System.out.println(noOfPmIds);
                if (noOfPmIds >= minPmIds) {
                    Gene thisHivGene = new Gene(rs.getInt("gId_1"));
                    Gene humanGene = library.getGene(rs.getInt("gId_2"));
                    String interaction = rs.getString("interaction");
                    int hivGeneIndex = hivGenes.indexOf(thisHivGene);
                    Gene hivGene;
                    if (hivGeneIndex < 0) {
                        hivGene = thisHivGene;
                        hivGenes.add(hivGene);
                        hivGene = thisHivGene;
                    } else {
                        hivGene = hivGenes.get(hivGeneIndex);
                    }
                    HivInteraction hivInteraction = new HivInteraction(humanGene, hivGene);
                    hivInteraction.setInteractionType(interaction);
                    hivInteractions.add(hivInteraction);
                }
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(HivData.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(!regulationOnly){
            sql = "select distinct gId_1, gId_2 from hiv_interactions where gId_1!='155807'";
        } else {
            sql = "select distinct gId_1, gId_2 from hiv_interactions where gId_1!='155807' and (interaction='upregulates' || interaction='downregulates' || interaction='regulates')";
        }
        rs = handle.runSQLquery(sql);
        try {
            while (rs.next()) {
                Gene thisHivGene = new Gene(rs.getInt("gId_1"));
                Gene humanGene = library.getGene(rs.getInt("gId_2"));
                int hivGeneIndex = hivGenes.indexOf(thisHivGene);
                Gene hivGene = hivGenes.get(hivGeneIndex);
                Interaction interaction = new Interaction(humanGene, hivGene);
                interactions.add(interaction);
                if (!genes.contains(humanGene)) {
                    genes.add(humanGene);
                }

            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(HivData.class.getName()).log(Level.SEVERE, null, ex);
        }
        Collections.sort(genes);

    }

    public List<Gene> getGenes() {
        return genes;
    }

    public List<Gene> getHivGenes() {
        return hivGenes;
    }

    public List<HivInteraction> getHivInteractions() {
        return hivInteractions;
    }

    public List<Interaction> getInteractions() {
        return interactions;
    }
}
